|
Download
FAQ History |
|
API
Search Feedback |
An Application Example that Deploys a Message-Driven Bean on Two J2EE Servers
This section, like the previous one, explains how to write, compile, package, deploy, and run a pair of J2EE applications that use the JMS API and run on two J2EE servers. The applications are slightly more complex than the ones in the first example.
The applications use the following components:
- An application client that uses two connection factories--one ordinary one and one that is configured to communicate with the remote server--to create two publishers and two subscribers and to publish and to consume messages
- A message-driven bean that is deployed twice--once on the local server and once on the remote one--to process the messages and to send replies
In this section, the term local server means the server on which both the application client and the message-driven bean are deployed. The term remote server means the server on which only the message-driven bean is deployed.
The section covers the following topics:
You will find the source files for this section in
<INSTALL>/j2eetutorial14/examples/jms/sendremote/. Path names in this section are relative to this directory.Overview of the Applications
This pair of applications is somewhat similar to the applications in An Application Example that Consumes Messages from a Remote J2EE Server in that the only components are a client and a message-driven bean. However, the applications here use these components in more complex ways. One application consists of the application client. The other application contains only the message-driven bean and is deployed twice, once on each server.
The basic steps of the applications are as follows.
- The administrator starts two J2EE servers, one on each system.
- On the local server, the administrator creates a connection factory to communicate with the remote server.
- The application client uses two connection factories--an ordinary one and the one just created--to create two connections, sessions, publishers, and subscribers. Each publisher publishes five messages.
- The local and the remote message-driven beans each receive five messages and send replies.
- The client's message listener consumes the replies.
Figure 21-5 illustrates the structure of this application.
![]()
Figure 21-5 A J2EE Application that Sends Messages to Two Servers
Writing the Application Components
Writing the components of the applications involves
Coding the Application Client: MultiAppServerClient.java
The application client class,
multiclient/src/MultiAppServerClient.java,does the following.
- It uses the JNDI naming context
java:comp/envto look up two connection factories and a topic.- For each connection factory, it creates a connection, a publisher session, a publisher, a subscriber session, a subscriber, and a temporary topic for replies.
- Each subscriber sets its message listener,
ReplyListener, and starts the connection.- Each publisher publishes five messages and creates a list of the messages the listener should expect.
- When each reply arrives, the message listener displays its contents and removes it from the list of expected messages.
- When all the messages have arrived, the client exits.
Coding the Message-Driven Bean: ReplyMsgBean.java
The
onMessagemethod of the message-driven bean class,replybean/src/ReplyMsgBean.java, does the following:On both servers, it will consume messages from the topic
jms/Topic.Creating and Packaging the Applications
This example uses the connection factory named
jms/TopicConnectionFactoryand the topic namedjms/Topic, both of which you created in Writing Simple JMS Client Applications. It uses an additional connection factory,jms/RemoteTopicConnectionFactory, that communicates with the remote system.Creating and packaging this application involve several steps:
Starting the J2EE Application Server and Compiling the Source Files
You need to start the J2EE Application Server on both the local and remote systems. You can perform all other tasks on the local system.
- Start the J2EE Application Server on the local system, if it is not already running.
- Start the J2EE Application Server on the remote system, if it is not already running.
- On the local system, go to the directory
multiclient. Compile the source files:
asant build- Also on the local system, go to the directory
replybean. Compile the source files:
asant buildCreating the Connection Factory
To create a connection factory that will communicate with the remote server, perform the following steps:
- Use the following command, replacing
sys-namewith the name of your remote system:
asadmin create-jms-resource --resourcetype
javax.jms.TopicConnectionFactory --property
imqAddressList=sys-name jms/RemoteTopicConnectionFactory- Reconfigure the server:
asant reconfig_common- To verify that the factory was created, use the following command:
asadmin list-jms-resources --user adminFor this example, the remote server must have the
TopicConnectionFactoryand topic you created in Writing Simple JMS Client Applications. If you have not run any of the tutorial examples on the remote system, run the followingasanttargets on it:Starting the J2EE Deploytool and Creating the Applications
You can perform all the packaging and deployment tasks on the local system.
Do the following to create the application for the client:
Do the following to create the application for the message-driven bean:
Packaging the Application Client
Perform the following steps to create and package the application client:
- Select
MultiClientAppand choose FileNew
Application Client JAR to start the Application Client Wizard.
- In the JAR File Contents screen:
- Verify that Create New AppClient Module in Application is selected and that the application is
MultiClientApp.- In the AppClient Display Name field, type
MultiAppServerClient.- Click the Edit button next to the Contents text area.
- In the dialog box, locate the
multiclient/build/directory. SelectMultiAppServerClient.classandMultiAppServerClient$ReplyListener.classfrom the Available Files tree area and click Add, then OK.- In the General screen, select
MultiAppServerClientin the Main Class combo box.- In the Message Destination References screen, click Add. In the dialog box:
- In the Resource References screen:
- Click Add.
- Type
jms/TopicConnectionFactory1in the Coded Name field.- Choose
javax.jms.ConnectionFactoryfrom the Type menu.- Type
jms/TopicConnectionFactoryin the JNDI name field andj2eein both the User Name and Password fields.- Click Add.
- Type
jms/TopicConnectionFactory2in the Coded Name field.- Choose
javax.jms.ConnectionFactoryfrom the Type menu.- Type
jms/RemoteTopicConnectionFactoryin the JNDI name field andj2eein both the User Name and Password fields.- Click Finish.
- Click the Message Destinations tab. In the inspector pane:
Packaging the Message-Driven Bean
Perform the following steps to package the message-driven bean:
- Select
ReplyBeanAppand choose FileNew
Enterprise JavaBean JAR to start the Enterprise Bean Wizard, then click Next.
- In the EJB JAR screen:
- Select Create New JAR Module in Application and verify that the application is
ReplyBeanApp.- In the JAR Display Name field, type
MDBJAR.- Click the Edit button next to the Contents text area.
- In the dialog box, locate the
replybean/build/directory. SelectReplyMsgBean.classfrom the Available Files tree area and click Add, then OK.- In the General screen:
- In the Configuration Options screen, check the Resource References and Transaction Management boxes.
- In the Message-Driven Bean Settings screen:
- Click Add in the Activation Configuration Properties area.
- Type
destinationin the Property Name field andPhysicalTopicin the Value field.- Click Add again.
- Type
destinationTypein the Property Name field andjavax.jms.Topicin the Value field.- Choose
javax.jms.Topicfrom the Destination Type menu.- Type
PhysicalTopicin the Destination Name field.- Click Deployment Settings. In the dialog box, type
jmsrain the Resource Adapter field and click OK.- In the Transaction Management screen, choose Container-Managed.
- In the Resource References screen:
- Click Finish.
- Click the Message Destinations tab. In the inspector pane:
Verifying and Updating the JNDI Names
You need to update the JNDI name for the
RemoteMDBAppapplication and verify it for theMultiClientAppapplication.The Application Pane for
ReplyBeanAppshould appear as shown in Table 21-10.
Table 21-10 Application Pane for ReplyBeanApp Component Type Component JNDI Name EJB ReplyMsgBean jms/Topic
The References Pane for
ReplyBeanAppshould appear as shown in Table 21-11.
Table 21-11 References Pane for ReplyBeanApp Ref. Type Referenced By Reference Name JNDI Name Resource ReplyMsgBean jms/TopicConnectionFactory jms/TopicConnectionFactory
Select the
MultiClientAppapplication and click the JNDI Names tab.The JNDI names for the application should appear as shown in Table 21-12. Only the References Pane has any content.
Deploying the Applications
To deploy the
MultiClientAppapplication and theReplyBeanAppapplication on the local server, perform the following steps for each application:
- Choose File
Save to save the application.
- Choose Tools
Deploy.
- In the dialog box, choose the URI for
localhostfrom the menu:
deployer:Sun:S1AS::localhost:4848- Type your administrative user name and password (if they are not already filled in) and click OK.
- In the Distribute Module dialog box, click Close when the process completes.
Before you can deploy the
ReplyBeanAppAppapplication, you must add the remote server, if you did not do so before. Perform the following steps:To deploy the
ReplyBeanAppapplication on the remote server, perform the following steps:
- Choose File
Save to save the application.
- Choose Tools
Deploy.
- In the dialog box, choose the URI with the name of the remote system from the menu.
- Type your administrative user name and password (if they are not already filled in) and click OK.
- In the Distribute Module dialog box, click Close when the process completes.
Saving the Client JAR and Running the Application Client
Perform the following steps:
- In the deploytool, select
localhost.- Select
MultiClientAppfrom the list of deployed objects, then click Client Jar.- Choose Browse to navigate to the directory from which you will run the client. Specify the
sendremote/multiclient/directory. When you reach the directory, click Select, then click OK.- Click OK in the information dialog. You will find a file named
MultiClientAppClient.jarin the specified directory.- To run the client, use the following command:
appclient -client MultiClientAppClient.jarOn the local system, the output of the
appclientcommand looks something like this:Binding name:`java:comp/env/jms/TopicName` Sent message: text: id=1 to local app server Sent message: text: id=2 to remote app server ReplyListener: Received message: id=1, text=ReplyMsgBean processed message: text: id=1 to local app server Sent message: text: id=3 to local app server ReplyListener: Received message: id=3, text=ReplyMsgBean processed message: text: id=3 to local app server ReplyListener: Received message: id=2, text=ReplyMsgBean processed message: text: id=2 to remote app server Sent message: text: id=4 to remote app server ReplyListener: Received message: id=4, text=ReplyMsgBean processed message: text: id=4 to remote app server Sent message: text: id=5 to local app server ReplyListener: Received message: id=5, text=ReplyMsgBean processed message: text: id=5 to local app server Sent message: text: id=6 to remote app server ReplyListener: Received message: id=6, text=ReplyMsgBean processed message: text: id=6 to remote app server Sent message: text: id=7 to local app server ReplyListener: Received message: id=7, text=ReplyMsgBean processed message: text: id=7 to local app server Sent message: text: id=8 to remote app server ReplyListener: Received message: id=8, text=ReplyMsgBean processed message: text: id=8 to remote app server Sent message: text: id=9 to local app server ReplyListener: Received message: id=9, text=ReplyMsgBean processed message: text: id=9 to local app server Sent message: text: id=10 to remote app server ReplyListener: Received message: id=10, text=ReplyMsgBean processed message: text: id=10 to remote app server Waiting for 0 message(s) from local app server Waiting for 0 message(s) from remote app server Finished Closing connection 1 Closing connection 2On the local system, where the message-driven bean receives the odd-numbered messages, the output in the server log looks like this:
In ReplyMsgBean.ReplyMsgBean() In ReplyMsgBean.setMessageDrivenContext() In ReplyMsgBean.ejbCreate() ReplyMsgBean: Received message: text: id=1 to local app server ReplyMsgBean: Received message: text: id=3 to local app server ReplyMsgBean: Received message: text: id=5 to local app server ReplyMsgBean: Received message: text: id=7 to local app server ReplyMsgBean: Received message: text: id=9 to local app serverOn the remote system, where the bean receives the even-numbered messages, the output in the server log looks like this:
In ReplyMsgBean.ReplyMsgBean() In ReplyMsgBean.setMessageDrivenContext() In ReplyMsgBean.ejbCreate() ReplyMsgBean: Received message: text: id=2 to remote app server ReplyMsgBean: Received message: text: id=4 to remote app server ReplyMsgBean: Received message: text: id=6 to remote app server ReplyMsgBean: Received message: text: id=8 to remote app server ReplyMsgBean: Received message: text: id=10 to remote app serverUndeploying and Removing the Applications
To undeploy and remove the applications, perform the following steps:
- Select
localhost.- Select
MultiClientAppin the server pane and click Undeploy.- Select the application in the left-hand pane and choose File
Close.
- Repeat steps 1-3 for
ReplyBeanAppon the local system.- Repeat steps 1-3 for
ReplyBeanAppon the remote system.- Remove the
builddirectory:
asant clean- If you wish, stop the server.
If you wish, you can manually delete the EAR and client JAR files.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.